package com.controller;
import com.entity.Comment;
import com.entity.Goods;
import com.entity.Report;
import com.service.CommentService;
import com.service.GoodsService;
import com.service.ReportService;
import com.util.CodeMsg;
import com.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ReportService reportService;

    /**
     * 获取商品评论列表
     */
    @GetMapping(value = "")
    public Result<List<Comment>> getComments(@RequestParam Long goodsId) {
        List<Comment> comments = commentService.findByGoodsId(goodsId);
        return Result.success(comments);
    }

    /**
     * 评论物品
     */
    @PostMapping(value = "")
    public Result<Boolean> addComment(@RequestBody Comment comment) {
        if (comment.getGoods() == null || comment.getGoods().getId() == null) {
            return Result.error(CodeMsg.GOODS_NOT_EXIST);
        }
        if (comment.getContent() == null || comment.getContent().trim().isEmpty()) {
            return Result.error(CodeMsg.COMMENT_CONTENT_EMPTY);
        }
        Goods goods = goodsService.findById(comment.getGoods().getId());
        if (goods == null) {
            return Result.error(CodeMsg.GOODS_NOT_EXIST);
        }

        comment.setGoods(goods);
        comment.setId(null);

        Comment savedComment = commentService.save(comment);

        if(savedComment == null) {
            return Result.error(CodeMsg.COMMENT_ADD_ERROR);
        } else {
            return Result.success(true);
        }
    }

    /**
     * 回复评论
     */
    @PostMapping(value = "/reply")
    public Result<Boolean> replyComment(@RequestBody Comment reply) {
        if (reply.getParent() == null || reply.getParent().getId() == null) {
            return Result.error(CodeMsg.PARENT_COMMENT_NOT_EXIST);
        }
        if (reply.getContent() == null || reply.getContent().trim().isEmpty()) {
            return Result.error(CodeMsg.COMMENT_CONTENT_EMPTY);
        }

        Comment parentComment = commentService.findById(reply.getParent().getId());
        if (parentComment == null) {
            return Result.error(CodeMsg.PARENT_COMMENT_NOT_EXIST);
        }

        reply.setGoods(parentComment.getGoods());
        reply.setId(null);

        Comment savedReply = commentService.save(reply);

        if(savedReply == null) {
            return Result.error(CodeMsg.REPLY_ADD_ERROR);
        } else {
            return Result.success(true);
        }
    }

    /**
     * 举报物品处理
     */
    @PostMapping(value = "/report")
    public Result<Boolean> reportGoods(@RequestBody Report reportGoods) {
        if(reportGoods.getGoods() == null || reportGoods.getGoods().getId() == null) {
            return Result.error(CodeMsg.GOODS_NOT_EXIST);
        }
        if (reportGoods.getContent() == null || reportGoods.getContent().trim().isEmpty()) {
            return Result.error(CodeMsg.REPORT_REASON_EMPTY);
        }
        
        Goods goods = goodsService.findById(reportGoods.getGoods().getId());
        if(goods == null) {
            return Result.error(CodeMsg.GOODS_NOT_EXIST);
        }
        
        reportGoods.setGoods(goods);
        reportGoods.setId(null);

        Report savedReport = reportService.save(reportGoods);

        if(savedReport == null) {
            return Result.error(CodeMsg.REPORT_ADD_ERROR); 
        } else {
            return Result.success(true);
        }
    }
}
